package com.ruoyi.service.jpjs.rlexportdata.impl;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.mapper.jpjs.PublicMapper;
import com.ruoyi.service.jpjs.proprogress.ProProgressService;
import com.ruoyi.service.jpjs.rlexportdata.RlExportProDataService;
import com.ruoyi.system.domain.SysUser;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 项目导出
 * @date 2020/6/11 10:03
 */
@Service
public class RlExportProDataServerImpl implements RlExportProDataService {

    @Autowired
    PublicMapper publicMapper;

    @Autowired
    ProProgressService proProgressService;

    @Override
    public AjaxResult exportProData(String years, String months) {
        try {
            HSSFWorkbook wb = new HSSFWorkbook();
            SysUser user = ShiroUtils.getSysUser();
            List<Map<String, Object>> maps = publicMapper.findUserCompAndJszb(user.getUserId());
            exportEmpChange(wb, years, months, maps);
            String filePath = Global.getDownloadPath() + "项目进度人员统计表.xls";
            FileOutputStream fileOut = new FileOutputStream(filePath);
            wb.write(fileOut);
            fileOut.close();
            return AjaxResult.success(0);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("失败");
        }
    }

    private void exportEmpChange(HSSFWorkbook wb, String years, String months, List<Map<String, Object>> maps) {
        //新增工作薄
        HSSFSheet sheet = wb.createSheet("项目进度、人员统计表");
        //新增一行
        HSSFRow row = sheet.createRow(0);
        //合并单元格
        HSSFCellStyle txtStyle1 = wb.createCellStyle();
        HSSFFont txtFont = wb.createFont();
        txtFont.setFontName("宋体");
        txtFont.setFontHeightInPoints((short) 9);
        txtFont.setBold(true);
        txtStyle1.setFont(txtFont);
        txtStyle1.setWrapText(true);
        txtStyle1.setAlignment(HorizontalAlignment.CENTER);
        txtStyle1.setVerticalAlignment(VerticalAlignment.CENTER);
        txtStyle1.setBorderBottom(BorderStyle.THIN);
        txtStyle1.setBorderLeft(BorderStyle.THIN);
        txtStyle1.setBorderTop(BorderStyle.THIN);
        txtStyle1.setBorderRight(BorderStyle.THIN);
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 2, 2));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 3, 3));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 4, 4));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 5, 5));
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 6, 8));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 9, 9));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 10, 10));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 11, 11));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 12, 12));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 13, 13));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 14, 14));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 15, 15));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 16, 16));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 17, 17));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 18, 18));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 19, 19));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 20, 20));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 21, 21));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 22, 22));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 23, 23));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 24, 24));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 25, 25));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 26, 26));


        row.createCell(0).setCellValue("区域");
        row.createCell(1).setCellValue("月份");
        row.createCell(2).setCellValue("年份");
        row.createCell(3).setCellValue("项目名称");
        row.createCell(4).setCellValue("项目业态");
        row.createCell(5).setCellValue("当前进度");
        row.createCell(6).setCellValue("建筑面积");
        row.createCell(9).setCellValue("配置");
        row.createCell(10).setCellValue("项目经理");
        row.createCell(11).setCellValue("技术负责人");
        row.createCell(12).setCellValue("生产经理");
        row.createCell(13).setCellValue("安全主管");
        row.createCell(14).setCellValue("安全员");
        row.createCell(15).setCellValue("质量主管");
        row.createCell(16).setCellValue("质检员");
        row.createCell(17).setCellValue("一级施工员");
        row.createCell(18).setCellValue("二级施工员");
        row.createCell(19).setCellValue("三级施工员");
        row.createCell(20).setCellValue("资料员");
        row.createCell(21).setCellValue("电工");
        row.createCell(22).setCellValue("材料主管");
        row.createCell(23).setCellValue("材料员");
        row.createCell(24).setCellValue("劳资员");
        row.createCell(25).setCellValue("合计人数");
        row.createCell(26).setCellValue("备注");
        for (Cell cell : row) {
            cell.setCellStyle(txtStyle1);
        }
        //新增一行
        HSSFRow row1 = sheet.createRow(1);
        row1.createCell(0);
        row1.createCell(1);
        row1.createCell(2);
        row1.createCell(3);
        row1.createCell(4);
        row1.createCell(5);
        row1.createCell(6).setCellValue("地上面积");
        row1.createCell(7).setCellValue("地下面积");
        row1.createCell(8).setCellValue("合计面积");
        row1.createCell(9);
        row1.createCell(10);
        row1.createCell(11);
        row1.createCell(12);
        row1.createCell(13);
        row1.createCell(14);
        row1.createCell(15);
        row1.createCell(16);
        row1.createCell(17);
        row1.createCell(18);
        row1.createCell(19);
        row1.createCell(20);
        row1.createCell(21);
        row1.createCell(22);
        row1.createCell(23);
        row1.createCell(24);
        row1.createCell(25);
        row1.createCell(26);
        for (Cell cell : row1) {
            cell.setCellStyle(txtStyle1);
        }

        HSSFCellStyle txtStyle = wb.createCellStyle();
        txtStyle.setWrapText(true);
        txtStyle.setAlignment(HorizontalAlignment.CENTER);
        txtStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        txtStyle.setBorderBottom(BorderStyle.THIN);
        txtStyle.setBorderLeft(BorderStyle.THIN);
        txtStyle.setBorderTop(BorderStyle.THIN);
        txtStyle.setBorderRight(BorderStyle.THIN);


        int excelRow = 2;
        int i = 0;
        HSSFRow row2;
        Map map = new HashMap();
        List<Map<String, Object>> proDataList = proProgressService.list(map);
        for (Map map1 : proDataList) {
            System.out.println(i);
            //合并前面的单元格
            sheet.addMergedRegion(new CellRangeAddress(excelRow, excelRow + 2, 0, 0));
            sheet.addMergedRegion(new CellRangeAddress(excelRow, excelRow + 2, 1, 1));
            sheet.addMergedRegion(new CellRangeAddress(excelRow, excelRow + 2, 2, 2));
            sheet.addMergedRegion(new CellRangeAddress(excelRow, excelRow + 2, 3, 3));
            sheet.addMergedRegion(new CellRangeAddress(excelRow, excelRow + 2, 4, 4));
            sheet.addMergedRegion(new CellRangeAddress(excelRow, excelRow + 2, 5, 5));
            sheet.addMergedRegion(new CellRangeAddress(excelRow, excelRow + 2, 6, 6));
            sheet.addMergedRegion(new CellRangeAddress(excelRow, excelRow + 2, 7, 7));
            sheet.addMergedRegion(new CellRangeAddress(excelRow, excelRow + 2, 8, 8));
            sheet.addMergedRegion(new CellRangeAddress(excelRow, excelRow + 2, 26, 26));

            //补充前面的数据
            row2 = sheet.createRow(excelRow);
            row2.createCell(0).setCellValue(map1.get("compName").toString());
            row2.createCell(1).setCellValue(map1.get("months").toString());
            row2.createCell(2).setCellValue(map1.get("years").toString());
            row2.createCell(3).setCellValue(map1.get("proName").toString());
            row2.createCell(4).setCellValue(map1.get("proFormat").toString());

            setHeight(row2,map1.get("proFormat").toString(),map1.get("currentProgress").toString());

            row2.createCell(5).setCellValue(map1.get("currentProgress").toString());
            row2.createCell(6).setCellValue(map1.get("groundArea").toString());
            row2.createCell(7).setCellValue(map1.get("undergroundArea").toString());
            row2.createCell(8).setCellValue(map1.get("totalArea").toString());
            row2.createCell(9).setCellValue("标准");
            //取标准数据
            Object obj = proProgressService.findPersonNumByTotalArea(Double.parseDouble(map1.get("totalArea").toString())).getData();
            Map map2 = (Map<String, Object>) obj;
            row2.createCell(10).setCellValue(map2.get("xmjl").toString());
            row2.createCell(11).setCellValue(map2.get("jsfzr").toString());
            row2.createCell(12).setCellValue(map2.get("scjl").toString());
            row2.createCell(13).setCellValue(map2.get("aqzg").toString());
            row2.createCell(14).setCellValue(map2.get("aqy").toString());
            row2.createCell(15).setCellValue(map2.get("zlzg").toString());
            row2.createCell(16).setCellValue(map2.get("zjy").toString());
            row2.createCell(17).setCellValue(map2.get("yjsgy").toString());
            row2.createCell(18).setCellValue(map2.get("erjsgy").toString());
            row2.createCell(19).setCellValue(map2.get("sjsgy").toString());
            row2.createCell(20).setCellValue(map2.get("zly").toString());
            row2.createCell(21).setCellValue(map2.get("dg").toString());
            row2.createCell(22).setCellValue(map2.get("clzg").toString());
            row2.createCell(23).setCellValue(map2.get("cly").toString());
            row2.createCell(24).setCellValue(map2.get("lzy").toString());
            row2.createCell(25).setCellValue(map2.get("hj").toString());
            row2.createCell(26).setCellValue(map1.get("remarks").toString());
            for (Cell cell : row2) {
                cell.setCellStyle(txtStyle);
            }

            //取实际数据
            row2 = sheet.createRow(excelRow + 1);
            row2.createCell(0);
            row2.createCell(1);
            row2.createCell(2);
            row2.createCell(3);
            row2.createCell(4);
            row2.createCell(5);
            row2.createCell(6);
            row2.createCell(7);
            row2.createCell(8);
            row2.createCell(9).setCellValue("实际");
            row2.createCell(10).setCellValue(map1.get("xmjlNum").toString());
            row2.createCell(11).setCellValue(map1.get("jsfzrNum").toString());
            row2.createCell(12).setCellValue(map1.get("scjlNum").toString());
            row2.createCell(13).setCellValue(map1.get("aqzgNum").toString());
            row2.createCell(14).setCellValue(map1.get("aqyNum").toString());
            row2.createCell(15).setCellValue(map1.get("zlzgNum").toString());
            row2.createCell(16).setCellValue(map1.get("zjyNum").toString());
            row2.createCell(17).setCellValue(map1.get("yjsgyNum").toString());
            row2.createCell(18).setCellValue(map1.get("erjsgyNum").toString());
            row2.createCell(19).setCellValue(map1.get("sjsgyNum").toString());
            row2.createCell(20).setCellValue(map1.get("zlyNum").toString());
            row2.createCell(21).setCellValue(map1.get("dgNum").toString());
            row2.createCell(22).setCellValue(map1.get("clzgNum").toString());
            row2.createCell(23).setCellValue(map1.get("clyNum").toString());
            row2.createCell(24).setCellValue(map1.get("lzyNum").toString());
            row2.createCell(25).setCellValue(map1.get("totalNum").toString());
            row2.createCell(26);
            for (Cell cell : row2) {
                cell.setCellStyle(txtStyle);
            }

            //取差异
            row2 = sheet.createRow(excelRow + 2);
            row2.createCell(0);
            row2.createCell(1);
            row2.createCell(2);
            row2.createCell(3);
            row2.createCell(4);
            row2.createCell(5);
            row2.createCell(6);
            row2.createCell(7);
            row2.createCell(8);
            row2.createCell(9).setCellValue("差异");
            row2.createCell(10).setCellValue(Integer.parseInt(map1.get("xmjlNum").toString()) - Integer.parseInt(map2.get("xmjl").toString()));
            row2.createCell(11).setCellValue(Integer.parseInt(map1.get("jsfzrNum").toString()) - Integer.parseInt(map2.get("jsfzr").toString()));
            row2.createCell(12).setCellValue(Integer.parseInt(map1.get("scjlNum").toString()) - Integer.parseInt(map2.get("scjl").toString()));
            row2.createCell(13).setCellValue(Integer.parseInt(map1.get("aqzgNum").toString()) - Integer.parseInt(map2.get("aqzg").toString()));
            row2.createCell(14).setCellValue(Integer.parseInt(map1.get("aqyNum").toString()) - Integer.parseInt(map2.get("aqy").toString()));
            row2.createCell(15).setCellValue(Integer.parseInt(map1.get("zlzgNum").toString()) - Integer.parseInt(map2.get("zlzg").toString()));
            row2.createCell(16).setCellValue(Integer.parseInt(map1.get("zjyNum").toString()) - Integer.parseInt(map2.get("zjy").toString()));
            row2.createCell(17).setCellValue(Integer.parseInt(map1.get("yjsgyNum").toString()) - Integer.parseInt(map2.get("yjsgy").toString()));
            row2.createCell(18).setCellValue(Integer.parseInt(map1.get("erjsgyNum").toString()) - Integer.parseInt(map2.get("erjsgy").toString()));
            row2.createCell(19).setCellValue(Integer.parseInt(map1.get("sjsgyNum").toString()) - Integer.parseInt(map2.get("sjsgy").toString()));
            row2.createCell(20).setCellValue(Integer.parseInt(map1.get("zlyNum").toString()) - Integer.parseInt(map2.get("zly").toString()));
            row2.createCell(21).setCellValue(Integer.parseInt(map1.get("dgNum").toString()) - Integer.parseInt(map2.get("dg").toString()));
            row2.createCell(22).setCellValue(Integer.parseInt(map1.get("clzgNum").toString()) - Integer.parseInt(map2.get("clzg").toString()));
            row2.createCell(23).setCellValue(Integer.parseInt(map1.get("clyNum").toString()) - Integer.parseInt(map2.get("cly").toString()));
            row2.createCell(24).setCellValue(Integer.parseInt(map1.get("lzyNum").toString()) - Integer.parseInt(map2.get("lzy").toString()));
            row2.createCell(25).setCellValue(Integer.parseInt(map1.get("totalNum").toString()) - Integer.parseInt(map2.get("hj").toString()));
            row2.createCell(26);
            for (Cell cell : row2) {
                cell.setCellStyle(txtStyle);
            }
            i++;
            excelRow = excelRow + 3;
            row2.setHeightInPoints(13);
        }
        sheet.setColumnWidth(0, 15 * 256);
        sheet.setColumnWidth(3, 15 * 256);
        sheet.setColumnWidth(4, 40 * 256);
        sheet.setColumnWidth(5, 40 * 256);
    }

    public void setHeight(HSSFRow row2,String a,String b){
        double aLength = a.length();
        double bLength = b.length();
        double cd = 0;
        if(aLength > bLength){
            cd = aLength;
        }else{
            cd = bLength;
        }
        cd = Math.ceil(cd / 67);
        row2.setHeightInPoints((int)cd * 20);
    }

}
